package com.chinasoft.bigdatadisplay.controller;

import com.chinasoft.bigdatadisplay.utils.SshExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/hadoop")
public class HadoopController {

    @Autowired
    private SshExecutor sshExecutor;

    /**
     * 调用该接口执行远程命令：hadoop jar elt.jar
     */
    @PostMapping("/execute-etl")
    public Map<String, Object> executeEltJob() {
        Map<String, Object> result = new HashMap<>();
        try {
            // 执行配置文件中定义的命令（也可直接写死"hadoop jar elt.jar"）
            String command = "hadoop jar /home/etl.jar";
            String commandOutput = sshExecutor.executeCommand(command);
            result.put("success", true);
            result.put("message", "命令执行成功");
            result.put("output", commandOutput);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "命令执行失败：" + e.getMessage());
        }
        return result;
    }
}